package com.example.dairenkun.neufoodshop.fragment;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.adapter.CollectShopAdapter;
import com.example.dairenkun.neufoodshop.bean.Collection;
import com.example.dairenkun.neufoodshop.model.FoodModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;

/**
 * A simple {@link Fragment} subclass.
 */
public class collect_shop_fragment extends Fragment {

    private RecyclerView recyclerView;
    private View view=null;
    int userid;
    public collect_shop_fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

       view= inflater.inflate(R.layout.fragment_collect_shop_fragment, container, false);
        initViews();
        initData();
        return  view;
    }
   private  void initViews(){

        recyclerView=(RecyclerView)view.findViewById(R.id.recycler_Collectshop_id);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    }
    private  void initData(){
        getUser();
        FoodModel foodModel=new FoodModel();
        Call<List<Collection>> call=foodModel.CollectList(userid,0);
        call.enqueue(new Callback<List<Collection>>() {
            @Override
            public void onResponse(Call<List<Collection>> call, retrofit2.Response<List<Collection>> response)
            {
                List<Collection> res=response.body();
                recyclerView.setAdapter(new CollectShopAdapter(getActivity(),res));
            }
            @Override
            public void onFailure(Call<List<Collection>> call, Throwable t) {

            }
        });
    }
    private void getUser(){
        SharedPreferences sp = getActivity().getSharedPreferences("UserInfo", getActivity().MODE_PRIVATE);
        userid = Integer.valueOf(sp.getString("user_id", null));
       // name=sp.getString("username",null);
    }
}
